import { Feed } from 'feed'
import SITE from '@/site.config'

export function generateRss(posts) {
  const year = new Date().getFullYear()
  const feed = new Feed({
    title:SITE.title,
    description:SITE.description,
    id: `${SITE.link}/${SITE.path}`,
    link: `${SITE.link}/${SITE.path}`,
    language:SITE.lang,
    favicon: `${SITE.link}/favicon.png`,
    copyright: `All rights reserved ${year}, ${SITE.author}`,
    author: {
      name:SITE.author,
      email:SITE.email,
      link:SITE.link
    }
  })
  posts.forEach(post => {
    feed.addItem({
      title: post.title,
      id: `${SITE.link}/${post.slug}`,
      link: `${SITE.link}/${post.slug}`,
      description: post.summary,
      date: new Date(post?.date?.start_date || post.createdTime)
    })
  })
  return feed.rss2()
}
